package com.chang.member.controller;

import com.chang.common.utils.PageUtils;
import com.chang.common.utils.R;
import com.chang.member.entity.MemberEntity;
import com.chang.member.feign.CouponFeignService;
import com.chang.member.service.MemberService;
import com.chang.member.vo.GiteeAccountVo;
import com.chang.member.vo.MemberLoginVo;
import com.chang.member.vo.MemberRegisterVo;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;



/**
 * 会员
 *
 * @author chang
 * @email 2045807586@qq.com
 * @date 2024-01-03 15:29:09
 */
@Slf4j
@RestController
@RequestMapping("member/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    @Autowired
    private CouponFeignService couponFeignService;

    @RequestMapping("coupons")
    public R test(){
        MemberEntity memberEntity = new MemberEntity();
        memberEntity.setNickname("牛至大王");
        R memberCoupons = couponFeignService.memberCoupons();
        return R.ok().put("member",memberEntity).put("coupons",memberCoupons.get("coupon"));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

    @PostMapping("register")
    public R register(@RequestBody MemberRegisterVo memberRegister) {
        // 原本我想把唯一性校验做一个远程调用，但是这样以来注册一个账号就需要远程调用member模块两次，比起请求一次redis存个验证码要得不偿失
        if (memberService.checkEmailUnique(memberRegister.getEmail()) || memberService.checkUsernameUnique(memberRegister.getUserName())) {
            return R.error("用户名或邮箱已被注册").put("Registered", true);
        }
        Boolean isReg = memberService.register(memberRegister);
        return R.ok().put("isReg", isReg);
    }


    @PostMapping("login")
    public R login(@Valid @RequestBody MemberLoginVo loginVo, BindingResult bindingResult) {
        log.info("MemberLoginVo=>{}", loginVo);
        if (bindingResult.hasErrors()) {
            return R.error(10001, "参数校验异常");
        }
        MemberEntity member = memberService.login(loginVo);
        if (member != null) {
            return R.ok().setData(member);
        }
        return R.error(10000, "密码错误或不存在该用户");
    }

    @PostMapping("/authLogin")
    public R oauth2Login(@RequestBody GiteeAccountVo giteeAccountVo) {
        log.info("giteeAccountVo=>{}", giteeAccountVo);
        MemberEntity member = memberService.login(giteeAccountVo);
        return R.ok();
    }

}
